package com.xinchuang.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinchuang.comment.KeyWord;
import com.xinchuang.comment.R;
import com.xinchuang.domain.entity.*;
import com.xinchuang.domain.pojo.TryOutDeviceMod;
import com.xinchuang.domain.type.NoticeMainEnum;
import com.xinchuang.domain.vo.NoteDetailVo;
import com.xinchuang.domain.vo.NoteListItemVo;
import com.xinchuang.entity.Edtion;
import com.xinchuang.entity.EdtionMessage;
import com.xinchuang.entity.api.OaUserParam;
import com.xinchuang.entity.api.TestModeApi;
import com.xinchuang.entity.api.UserApi;
import com.xinchuang.manager.*;
import com.xinchuang.mapper.*;
import com.xinchuang.role.entity.SystemHeader;
import com.xinchuang.service.NoteModService;
import com.xinchuang.service.OaUserService;
import com.xinchuang.service.actual.EdtionMessageService;
import com.xinchuang.service.actual.EdtionService;
import com.xinchuang.service.apiutil.impl.ApiUtilServiceImpl;
import com.xinchuang.utils.EdtionUtil;
import com.xinchuang.utils.EdtionVerity;
import com.xinchuang.utils.IdUtils;
import com.xinchuang.utils.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;
import java.text.SimpleDateFormat;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Service
public class NoteModServiceImpl implements NoteModService {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private TestNoticeMainTabaleService testNoticeMainTabaleService;

    @Autowired
    private TestModelMainTableService testModelMainTableService;

    @Autowired
    private TestModelParamService testModelParamService;

    @Autowired
    private TestModelSubTableService testModelSubTableService;

    @Autowired
    private TestNoticeMainTabaleMapper testNoticeMainTabaleMapper;

    @Autowired
    private DictTableMapper dictTableMapper;

    @Autowired
    private TryOutDeviceModMapper tryOutDeviceModMapper;

    @Autowired
    private UserApiMapper userApiMapper;

    @Autowired
    private KeyWord keyWord;

    @Autowired
    private EdtionService edtionService;

    @Autowired
    private EdtionMessageService edtionMessageService;

    @Autowired
    private OaUserService oaUserService;
    @Autowired
    private TestImageService testImageService;
    @Autowired
    private TestImageMapper testImageMapper;

    /**
     * 创建SimpleDateFormat对象，指定日期格式为ISO 8601
     */
    SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    @Override
    @Transactional
    public synchronized String editNote(JSONObject jsonObject, HttpServletRequest request) {
        //jsonobject解析获取模具名称
        String moldName = jsonObject.getString("moldName");
        //jsonobject解析获取模具外形
        String moldLook = jsonObject.getString("moldLook");
        //jsonobject解析获取几点浇注
        String pourTime = jsonObject.getString("pourTime");
        //jsonobject解析获取几点温控（注明接口）
        String temperatureControlTime = jsonObject.getString("temperatureControlTime");
        //jsonobject解析获取是否需要液压工作站
        String needHydraumaticStation = jsonObject.getString("needHydraumaticStation");
        //jsonobject解析获取几点顺序注塑
        String injectionMoldingTime = jsonObject.getString("injectionMoldingTime");
        //jsonobject解析获取是否有限位开关
        String hasLimitSwitch = jsonObject.getString("hasLimitSwitch");
        //jsonobject解析获取试模设备Id
        String tryOutDeviceId = jsonObject.getString("tryOutDeviceId");
        //jsonobject解析获取热点偶型号（J或K）
        String hotSpotPairType = jsonObject.getString("hotSpotPairType");
        //jsonobject解析获取模具编号
        String moldId = jsonObject.getString("moldId");
        //jsonobject解析获取版本Id
        String versionId = jsonObject.getString("versionId");
        //jsonobject解析获取通知单名称
        String noteName = jsonObject.getString("noteName");
        //jsonobject解析获取编号
        String serialNumber = jsonObject.getString("serialNumber");
        //jsonobject解析获取是否有时间继电器
        String hasTimeRelay = jsonObject.getString("hasTimeRelay");
        //jsonobject解析获取制件材料
        String materials = jsonObject.getString("materials");
        //jsonobject解析获取试模件数
        String moldCount = jsonObject.getString("moldCount");
        //jsonobject解析获取制件颜色
        String color = jsonObject.getString("color");
        //jsonobject解析获取材料重量
        String materialsCount = jsonObject.getString("materialsCount");
        //jsonobject解析获取样件重量
        String samplePieceWeight = jsonObject.getString("samplePieceWeight");
        //jsonobject解析获取试模次数
        String tryOutTimes = jsonObject.getString("tryOutTimes");
        //jsonobject解析获取计划试模时间
        String plannedTime = jsonObject.getString("plannedTime");
        //jsonobject解析获取模具重量
        String moldWeight = jsonObject.getString("moldWeight");
        //jsonobject解析获取钳工组长
        JSONArray tongmanName = jsonObject.getJSONArray("tongmanName");
        //jsonobject解析获取试模目的Id
        String targetId = jsonObject.getString("targetId");
        //jsonobject解析获取副试模目的
        String subTarget = jsonObject.getString("subTarget");
        //jsonobject解析获取试模原因Id
        String tryOutReasonId = jsonObject.getString("tryOutReasonId");
        //jsonobject解析获取试模注意事项有无特殊要求
        String announcements = jsonObject.getString("announcements");
        //jsonobject解析获取注意事项上传图片路径
        String announcementsImages = jsonObject.getString("announcementsImages");
        JSONArray imageArray = jsonObject.getJSONArray("images");
        //jsonobject解析获取项目名称
        String projectName = jsonObject.getString("projectName");
        //jsonobject解析获取拟制人
//        String projectManager = jsonObject.getString("projectManager");
        SystemHeader header = MyUtils.getHeader(request);
//        String projectManager = MyUtils.getName(header.getId());
        //jsonobject解析获取试模通知单Id
        String noteId = jsonObject.getString("noteId");

        if (!StringUtils.isNumeric(moldCount)) {
            return JSON.toJSONString(R.fail("请确认试模件数,填写是否为数字"));
        }

//        List<Edtion> list = new ArrayList<>();

        TestNoticeMainTabale mainTabale = testNoticeMainTabaleService.getById(noteId);
        if (ObjectUtil.isNotNull(mainTabale)) {
            if (StringUtils.isNotBlank(mainTabale.getStatus())) {
                switch (mainTabale.getStatus()) {
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                        return JSON.toJSONString(R.fail(500, "通知单已提交，不可修改"));
                }
            }
//            int vs;
//            if (null == mainTabale.getEdition()) {
//                vs = 2;
//            } else {
//                vs = mainTabale.getEdition() + 1;
//            }
            TestModelSubTable subTable = testModelSubTableService.getById(mainTabale.getParentPlanSubId());
            if (ObjectUtil.isNotNull(subTable)) {
                if (StringUtils.isNotBlank(tryOutTimes)) {
                    if (!tryOutTimes.toUpperCase().contains("T")
                            || !StringUtils.isNumeric(tryOutTimes.toUpperCase().split("T")[1])) {
                        return JSON.toJSONString(R.fail("请确认试模次数,填写是否为T+数字的格式,如:T0、T1等"));
                    }
                    subTable.setTryOutTimes(tryOutTimes.toUpperCase().split("T")[1]);
                }
//                List<Edtion> edtions = EdtionVerity.verityNoticeMainTabale(
//                        moldId,
//                        moldName,
//                        moldLook,
//                        pourTime,
//                        temperatureControlTime,
//                        needHydraumaticStation,
//                        injectionMoldingTime,
//                        hasLimitSwitch,
//                        hotSpotPairType,
//                        hasTimeRelay,
//                        materials,
//                        moldCount,
//                        color,
//                        materialsCount,
//                        samplePieceWeight,
//                        moldWeight,
//                        tongmanName,
//                        tryOutTimes,
//                        plannedTime,
//                        mainTabale.getId(),
//                        mainTabale.getEdition(),
//                        header.getId(),
//                        subTable,
//                        vs
//                );
//                if (CollectionUtil.isNotEmpty(edtions)) {
//                    list.addAll(edtions);
//                }
                if (StringUtils.isNotBlank(moldId)) {
                    subTable.setMoldId(moldId);
                }
                if (StringUtils.isNotBlank(moldName)) {
                    subTable.setMoldName(moldName);
                }
                if (StringUtils.isNotBlank(moldLook)) {
                    subTable.setMoldLook(moldLook);
                }
                if (StringUtils.isNotBlank(pourTime)) {
                    subTable.setPourTime(pourTime);
                }
                if (StringUtils.isNotBlank(temperatureControlTime)) {
                    subTable.setTemperatureControlTime(temperatureControlTime);
                }
                if (StringUtils.isNotBlank(needHydraumaticStation)) {
                    subTable.setNeedHydraumaticStation(needHydraumaticStation);
                }
                if (StringUtils.isNotBlank(injectionMoldingTime)) {
                    subTable.setInjectionMoldingTime(injectionMoldingTime);
                }
                if (StringUtils.isNotBlank(hasLimitSwitch)) {
                    subTable.setHasLimitSwitch(hasLimitSwitch);
                }
                if (StringUtils.isNotBlank(hotSpotPairType)) {
                    subTable.setHotSpotPairType(hotSpotPairType);
                }
                if (StringUtils.isNotBlank(hasTimeRelay)) {
                    subTable.setHasTimeRelay(hasTimeRelay);
                }
                if (StringUtils.isNotBlank(materials)) {
                    subTable.setMaterials(materials);
                    subTable.setMaterialName(materials);
                }
                if (StringUtils.isNotBlank(moldCount)) {
                    subTable.setMoldCount(moldCount);
                    subTable.setTryOutDeviceCount(Integer.valueOf(moldCount));
                }
                if (StringUtils.isNotBlank(color)) {
                    subTable.setColor(color);
                }
                if (StringUtils.isNotBlank(materialsCount)) {
                    subTable.setMaterialsCount(materialsCount);
                    subTable.setMaterialWeight(materialsCount);
                }
                if (StringUtils.isNotBlank(samplePieceWeight)) {
                    subTable.setSamplePieceWeight(samplePieceWeight);
                }
                if (StringUtils.isNotBlank(moldWeight)) {
                    subTable.setMoldWeight(moldWeight);
                }
                if (CollectionUtil.isNotEmpty(tongmanName)) {
                    subTable.setTongmanId(tongmanName.getString(0));
                    subTable.setTongmanName(tongmanName.getString(1));
                }
                if (StringUtils.isNotBlank(plannedTime)) {
                    subTable.setPlannedTime(MyUtils.stringToDate(plannedTime));
                }
                if (StringUtils.isNotBlank(projectName)) {
                    subTable.setProjectName(projectName);
                }
                //主表
//                if (null != versionId && !versionId.equals(mainTabale.getVersionId())) {
//                    Edtion edtion = new Edtion();
//                    edtion.setRed_id(mainTabale.getId());
//                    edtion.setEdition("V" + vs);
//                    edtion.setKeyName("versionId");
//                    edtion.setKeyValue("版本");
//                    DictTable dictTable = dictTableMapper.selectById(mainTabale.getVersionId());
//                    if (ObjectUtil.isEmpty(dictTable)) {
//                        edtion.setBaseValue("");
//                    } else {
//                        edtion.setBaseValue(dictTable.getDicName());
//                    }
//                    DictTable dictTable1 = dictTableMapper.selectById(versionId);
//                    if (ObjectUtil.isEmpty(dictTable1)) {
//                        edtion.setUpdateValue("");
//                    } else {
//                        edtion.setUpdateValue(dictTable1.getDicName());
//                    }
//                    edtion.setPerson(header.getId());
//                    list.add(edtion);
//                }
//                if (null != noteName && !noteName.equals(mainTabale.getNoteName())) {
//                    Edtion edtion = new Edtion();
//                    edtion.setRed_id(mainTabale.getId());
//                    edtion.setEdition("V" + vs);
//                    edtion.setKeyName("note_name");
//                    edtion.setKeyValue("通知单名称");
//                    edtion.setBaseValue(mainTabale.getNoteName());
//                    edtion.setUpdateValue(noteName);
//                    edtion.setPerson(header.getId());
//                    list.add(edtion);
//                }
//                if (null != serialNumber && !serialNumber.equals(mainTabale.getSerialNumber())) {
//                    Edtion edtion = new Edtion();
//                    edtion.setRed_id(mainTabale.getId());
//                    edtion.setEdition("V" + vs);
//                    edtion.setKeyName("serial_number");
//                    edtion.setKeyValue("编号");
//                    edtion.setBaseValue(mainTabale.getSerialNumber());
//                    edtion.setUpdateValue(serialNumber);
//                    edtion.setPerson(header.getId());
//                    list.add(edtion);
//                }
//                if (null != targetId && !targetId.equals(mainTabale.getTargetId())) {
//                    Edtion edtion = new Edtion();
//                    edtion.setRed_id(mainTabale.getId());
//                    edtion.setEdition("V" + vs);
//                    edtion.setKeyName("targetId");
//                    edtion.setKeyValue("试模目的");
//                    DictTable dictTable = dictTableMapper.selectById(mainTabale.getTargetId());
//                    if (ObjectUtil.isEmpty(dictTable)) {
//                        edtion.setBaseValue("");
//                    } else {
//                        edtion.setBaseValue(dictTable.getDicName());
//                    }
//                    DictTable dictTable1 = dictTableMapper.selectById(targetId);
//                    if (ObjectUtil.isEmpty(dictTable1)) {
//                        edtion.setUpdateValue("");
//                    } else {
//                        edtion.setUpdateValue(dictTable1.getDicName());
//                    }
//                    edtion.setPerson(header.getId());
//                    list.add(edtion);
//                }
//                if (null != tryOutReasonId && !tryOutReasonId.equals(mainTabale.getTryOutReasonId())) {
//                    Edtion edtion = new Edtion();
//                    edtion.setRed_id(mainTabale.getId());
//                    edtion.setEdition("V" + vs);
//                    edtion.setKeyName("tryOutReasonId");
//                    edtion.setKeyValue("试模原因");
//                    DictTable dictTable = dictTableMapper.selectById(mainTabale.getTryOutReasonId());
//                    if (ObjectUtil.isEmpty(dictTable)) {
//                        edtion.setBaseValue("");
//                    } else {
//                        edtion.setBaseValue(dictTable.getDicName());
//                    }
//                    DictTable dictTable1 = dictTableMapper.selectById(tryOutReasonId);
//                    if (ObjectUtil.isEmpty(dictTable1)) {
//                        edtion.setUpdateValue("");
//                    } else {
//                        edtion.setUpdateValue(dictTable1.getDicName());
//                    }
//                    edtion.setPerson(header.getId());
//                    list.add(edtion);
//                }
//                if (null != announcements && !announcements.equals(mainTabale.getAnnouncements())) {
//                    Edtion edtion = new Edtion();
//                    edtion.setRed_id(mainTabale.getId());
//                    edtion.setEdition("V" + vs);
//                    edtion.setKeyName("announcements");
//                    edtion.setKeyValue("试模注意事项");
//                    edtion.setBaseValue(mainTabale.getAnnouncements());
//                    edtion.setUpdateValue(announcements);
//                    edtion.setPerson(header.getId());
//                    list.add(edtion);
//                }
//                if (null != subTarget && !subTarget.equals(mainTabale.getSubTarget())) {
//                    Edtion edtion = new Edtion();
//                    edtion.setRed_id(mainTabale.getId());
//                    edtion.setEdition("V" + vs);
//                    edtion.setKeyName("subTarget");
//                    edtion.setKeyValue("副试模目的");
//                    edtion.setBaseValue(mainTabale.getSubTarget());
//                    edtion.setUpdateValue(subTarget);
//                    edtion.setPerson(header.getId());
//                    list.add(edtion);
//                }
//                    if (null != tryOutDeviceId && !tryOutDeviceId.equals(mainTabale.getTryOutDeviceId())) {
//                    Edtion edtion = new Edtion();
//                    edtion.setRed_id(mainTabale.getId());
//                    edtion.setEdition("V" + vs);
//                    edtion.setKeyName("tryOutDeviceId");
//                    edtion.setKeyValue("试模设备");
//                    TryOutDeviceMod deviceMod = tryOutDeviceModMapper.selectById(mainTabale.getTryOutDeviceId());
//                    if (ObjectUtil.isEmpty(deviceMod)) {
//                        edtion.setBaseValue("");
//                    } else {
//                        edtion.setBaseValue(deviceMod.getDeviceName());
//                    }
//                    TryOutDeviceMod deviceMod1 = tryOutDeviceModMapper.selectById(mainTabale.getTryOutDeviceId());
//                    if (ObjectUtil.isEmpty(deviceMod1)) {
//                        edtion.setUpdateValue("");
//                    } else {
//                        edtion.setUpdateValue(deviceMod1.getDeviceName());
//                    }
//                    edtion.setPerson(header.getId());
//                    list.add(edtion);
//                }
//                if (CollectionUtil.isNotEmpty(list)) {
//                    edtionService.saveBatch(list);
//                    mainTabale.setEdition(vs);
//                }
                testNoticeMainTabaleService.lambdaUpdate()
                        .eq(TestNoticeMainTabale::getId, noteId)
                        .set(TestNoticeMainTabale::getVersionId, versionId)
                        .set(TestNoticeMainTabale::getNoteName, noteName)
                        .set(TestNoticeMainTabale::getSerialNumber, serialNumber)
                        .set(TestNoticeMainTabale::getTargetId, targetId)
                        .set(TestNoticeMainTabale::getTryOutTypeId, targetId)
                        .set(TestNoticeMainTabale::getTryOutReasonId, tryOutReasonId)
                        .set(TestNoticeMainTabale::getAnnouncements, announcements)
                        .set(TestNoticeMainTabale::getAnnouncementsImages, announcementsImages)
                        .set(TestNoticeMainTabale::getSubTarget, subTarget)
                        .set(TestNoticeMainTabale::getTryOutDeviceId, tryOutDeviceId)
                        .set(TestNoticeMainTabale::getEdition, mainTabale.getEdition())
                        .set(TestNoticeMainTabale::getUpdateTime, new Date())
                        .update();
                subTable.setUpdateTime(new Date());
                testModelSubTableService.updateById(subTable);

                //保存图片
                List<String> imageList = JSONObject.parseArray(imageArray.toJSONString(), String.class);
                if (Objects.nonNull(imageList) && !imageList.isEmpty()) {
                    testImageMapper.delete(new LambdaUpdateWrapper<TestImage>().eq(TestImage::getType, 2).eq(TestImage::getSceneId, noteId));
                    List<TestImage> testImageList = imageList.stream().map(url -> {
                                TestImage testImage = new TestImage();
                                testImage.setType(2);
                                testImage.setSceneId(noteId);
                                testImage.setUrl(url);
                                return testImage;
                            })
                            .collect(Collectors.toList());
                    testImageService.saveBatch(testImageList);
                }
            }
        }
        return JSON.toJSONString(R.success("修改成功"));
    }

    @Override
    public R getNoteList(JSONObject jsonObject, HttpServletRequest request) {
        //jsonobject解析获取计划试模开始时间
        String starttime = jsonObject.getString("starttime");
        //jsonobject解析获取计划试模结束时间
        String endtime = jsonObject.getString("endtime");
        //jsonobject解析获取模具编号
        String moldId = jsonObject.getString("moldId");
        //jsonobject解析获取版本Id
        String versionId = jsonObject.getString("versionId");
        //jsonobject解析获取页码
        Long pageNum = jsonObject.getLong("pageNum");
        //jsonobject解析获取每页数量
        Long pageSize = jsonObject.getLong("pageSize");
        //jsonobject解析获取模具名称
        String moldName = jsonObject.getString("moldName");
        //jsonobject解析获取项目负责人
        String username = jsonObject.getString("username");
        String tryoutStatus = jsonObject.getString("tryoutStatus");
        String tongName = jsonObject.getString("tongName");
        String projectName = jsonObject.getString("projectName");
        Page<NoteListItemVo> page = new Page<>(pageNum, pageSize);
        SystemHeader header = MyUtils.getHeader(request);
        //获取用户ID
        UserApi userApi = userApiMapper.selectById(header.getId());
        Integer userId = -1;
        if (ObjectUtil.isNotNull(userApi)) {
            userId = userApi.getUserId();
        }
        List<String> list = null;
        if (StringUtils.isNotBlank(tryoutStatus)) {
            list = new ArrayList<>();
            switch (tryoutStatus) {
                case "0":
                    list.add("0");
                    list.add("5");
                    break;
                case "1":
                    list.add("4");
                    break;
                case "2":
                    list.add("6");
                    break;
                case "3":
                    list.add("7");
                    break;
                case "4":
                    list.add("8");
                    break;
                case "5":
                    list.add("1");
                    list.add("2");
                    list.add("3");
                    break;
                default:
                    break;
            }
        }
        List<String> tn = null;
        if (StringUtils.isNotBlank(tongName)) {
            List<OaUserParam> params = oaUserService.lambdaQuery().like(OaUserParam::getName, tongName).list();
            if (CollectionUtil.isNotEmpty(params)) {
                tn = new ArrayList<>();
                for (OaUserParam param : params) {
                    tn.add(param.getId() + "");
                }
            }
        }
        IPage<NoteListItemVo> noteList = testNoticeMainTabaleMapper.getNoteList(
                page,
                starttime, endtime, moldId, moldName, versionId, userId, username, list, tn, projectName
        );
        HashMap<String, Object> data = new HashMap<>();
        if (CollectionUtil.isNotEmpty(noteList.getRecords())) {
            JSONArray array = new JSONArray();
            for (NoteListItemVo record : noteList.getRecords()) {
                JSONObject object = JSONObject.parseObject(JSON.toJSONString(record));
                if (ObjectUtil.isNotNull(object.getLong("plannedTime"))) {
                    object.put("plannedTime", DateUtil.format(new Date(object.getLong("plannedTime")), "yyyy-MM-dd"));
                }
                if (StringUtils.isNotBlank(object.getString("tryOutTimes"))) {
                    object.put("tryOutTimes", "T" + record.getTryOutTimes());
                }
                if (StringUtils.isBlank(object.getString("noteName"))) {
                    object.put("noteName", record.getMoldName() + "_T" + record.getTryOutTimes() + "_试模通知单");
                }
                object.put("tryoutStatus", NoticeMainEnum.getMessageValue(record.getStatus()));
                object.put("status", NoticeMainEnum.getMessageName(record.getStatus()));
                object.put("projectManagerName", oaUserService.getOaUserName(record.getProjectManager()));
                object.put("projectName", MyUtils.getString(record.getProjectName()));
                object.put("tongmanNameName", oaUserService.getOaUserName(record.getTongmanName()));
                object.put("edition", "V" + (null == record.getEdition() ? 1 : record.getEdition()));
                array.add(object);
            }
            data.put("dataList", array);
            data.put("totalNum", noteList.getTotal());
        } else {
            data.put("dataList", new ArrayList<>());
            data.put("totalNum", 0);
        }
        return R.data(data);
    }

    @Override
    @Transactional
    public String addNote(JSONObject jsonObject, HttpServletRequest request) {
        //jsonobject解析获取试模设备Id
        String tryOutDeviceId = jsonObject.getString("tryOutDeviceId");
        //jsonobject解析获取是否有限位开关
        String hasLimitSwitch = jsonObject.getString("hasLimitSwitch");
        //jsonobject解析获取几点顺序注塑
        String injectionMoldingTime = jsonObject.getString("injectionMoldingTime");
        //jsonobject解析获取是否需要液压工作站
        String needHydraumaticStation = jsonObject.getString("needHydraumaticStation");
        //jsonobject解析获取热点偶型号（J或K）
        String hotSpotPairType = jsonObject.getString("hotSpotPairType");
        //jsonobject解析获取几点温控（注明接口）
        String temperatureControlTime = jsonObject.getString("temperatureControlTime");
        //jsonobject解析获取几点浇注
        String pourTime = jsonObject.getString("pourTime");
        //jsonobject解析获取模具外形
        String moldLook = jsonObject.getString("moldLook");
        //jsonobject解析获取模具名称
        String moldName = jsonObject.getString("moldName");
        //jsonobject解析获取模具编号
        String moldId = jsonObject.getString("moldId");
        //jsonobject解析获取版本Id
        String versionId = jsonObject.getString("versionId");
        //jsonobject解析获取通知单名称
        String noteName = jsonObject.getString("noteName");
        //jsonobject解析获取编号
        String serialNumber = jsonObject.getString("serialNumber");
        //jsonobject解析获取是否有时间继电器
        String hasTimeRelay = jsonObject.getString("hasTimeRelay");
        //jsonobject解析获取制件材料
        String materials = jsonObject.getString("materials");
        //jsonobject解析获取试模件数
        String moldCount = jsonObject.getString("moldCount");
        //jsonobject解析获取制件颜色
        String color = jsonObject.getString("color");
        //jsonobject解析获取材料重量
        String materialsCount = jsonObject.getString("materialsCount");
        //jsonobject解析获取样件重量
        String samplePieceWeight = jsonObject.getString("samplePieceWeight");
        //jsonobject解析获取试模次数
        String tryOutTimes = jsonObject.getString("tryOutTimes");
        //jsonobject解析获取计划试模时间
        String plannedTime = jsonObject.getString("plannedTime");
        //jsonobject解析获取模具重量
        String moldWeight = jsonObject.getString("moldWeight");
        //jsonobject解析获取钳工组长
        JSONArray tongmanArray = jsonObject.getJSONArray("tongmanName");
        //jsonobject解析获取试模目的Id
        String targetId = jsonObject.getString("targetId");
        //jsonobject解析获取副试模目的
        String subTarget = jsonObject.getString("subTarget");
        //jsonobject解析获取试模原因Id
        String tryOutReasonId = jsonObject.getString("tryOutReasonId");
        //jsonobject解析获取试模注意事项有无特殊要求
        String announcements = jsonObject.getString("announcements");
        //jsonobject解析获取注意事项上传图片路径
        String announcementsImages = jsonObject.getString("announcementsImages");
        JSONArray imageArray = jsonObject.getJSONArray("images");
        //jsonobject解析获取项目名称
        String projectName = jsonObject.getString("projectName");
        //jsonobject解析获取拟制人
//        String projectManager = jsonObject.getString("projectManager");
        //jsonobject解析获取审核人
//        String checkoutName = jsonObject.getString("checkoutName");
        //jsonobject解析获取批准(人)
//        String approver = jsonObject.getString("approver");
        if (StringUtils.isNotBlank(tryOutTimes)) {
            if (!tryOutTimes.toUpperCase().contains("T")
                    || !StringUtils.isNumeric(tryOutTimes.toUpperCase().split("T")[1])) {
                return JSON.toJSONString(R.fail("请确认试模次数,填写是否为T+数字的格式,如:T0、T1等"));
            }
        }
        if (!StringUtils.isNumeric(moldCount)) {
            return JSON.toJSONString(R.fail("请确认试模件数,填写是否为数字"));
        }
        TestModelParam testModelParam = testModelParamService.getById(moldId);
        if (ObjectUtil.isNull(testModelParam)) {
            return JSON.toJSONString(R.fail("当前模型数据不存在"));
        }
        String noteId = IdUtils.simpleUUID();
        String parent_plan_sub_id = IdUtils.simpleUUID();
        TestNoticeMainTabale testNoticeMainTabale = new TestNoticeMainTabale();
        testNoticeMainTabale.setId(noteId);
        testNoticeMainTabale.setParentPlanSubId(parent_plan_sub_id);
        testNoticeMainTabale.setNoteName(noteName);
        testNoticeMainTabale.setSerialNumber(serialNumber);
        testNoticeMainTabale.setVersionId(versionId);
        testNoticeMainTabale.setTargetId(targetId);
        testNoticeMainTabale.setTryOutTypeId(targetId);
        testNoticeMainTabale.setSubTarget(subTarget);
        testNoticeMainTabale.setTryOutReasonId(tryOutReasonId);
        testNoticeMainTabale.setTryOutDeviceId(tryOutDeviceId);
        testNoticeMainTabale.setTryOutDeviceId(tryOutDeviceId);
        testNoticeMainTabale.setAnnouncements(announcements);
        testNoticeMainTabale.setAnnouncementsImages(announcementsImages);
        SystemHeader header = MyUtils.getHeader(request);
        UserApi userApi = userApiMapper.selectById(header.getId());
        testNoticeMainTabale.setProjectManager(userApi.getName());
        testNoticeMainTabale.setTryOutCompanyId("0");
        testNoticeMainTabale.setTongman(Integer.valueOf(header.getId()));
        testNoticeMainTabale.setEdition(0);
        testNoticeMainTabale.setCreateTime(new Date());
        testNoticeMainTabale.setUpdateTime(new Date());
        testNoticeMainTabaleService.save(testNoticeMainTabale);

        TestModelSubTable testModelSubTable = new TestModelSubTable();
        testModelSubTable.setPlanId(parent_plan_sub_id);
        testModelSubTable.setMoldId(moldId);
        testModelSubTable.setMoldName(moldName);
        testModelSubTable.setCustomerName(testModelParam.getCustomerName());
        if (CollectionUtil.isNotEmpty(tongmanArray)) {
            testModelSubTable.setTongmanId(tongmanArray.getString(0));
            testModelSubTable.setTongmanName(tongmanArray.getString(1));
        }
        testModelSubTable.setMoldLook(moldLook);
        testModelSubTable.setPourTime(pourTime);
        testModelSubTable.setPourTime(pourTime);
        testModelSubTable.setTemperatureControlTime(temperatureControlTime);
        testModelSubTable.setNeedHydraumaticStation(needHydraumaticStation);
        testModelSubTable.setInjectionMoldingTime(injectionMoldingTime);
        testModelSubTable.setHasLimitSwitch(hasLimitSwitch);
        testModelSubTable.setHotSpotPairType(hotSpotPairType);
        testModelSubTable.setHasTimeRelay(hasTimeRelay);
        testModelSubTable.setMaterials(materials);
        testModelSubTable.setMaterialName(materials);
        testModelSubTable.setMoldCount(moldCount);
        testModelSubTable.setColor(color);
        testModelSubTable.setMaterialsCount(materialsCount);
        testModelSubTable.setMaterialWeight(materialsCount);
        testModelSubTable.setSamplePieceWeight(samplePieceWeight);
        testModelSubTable.setMoldWeight(moldWeight);
        testModelSubTable.setMaterialWeight(materialsCount);
        OaUserParam oaUserParam = oaUserService.getById(userApi.getId());
        testModelSubTable.setProjectManagerId(oaUserParam.getUnitId() + "");
        testModelSubTable.setProjectManagerName(oaUserParam.getId() + "");
        try {
            testModelSubTable.setPlannedTime(StringUtils.isBlank(plannedTime) ? null : sdf.parse(plannedTime));
        } catch (ParseException e) {
            return JSON.toJSONString(R.fail("预计试模时间格式异常，请重新输入"));
        }
        testModelSubTable.setProjectName(projectName);
        if (StringUtils.isNotBlank(tryOutTimes)) {
            if (tryOutTimes.toUpperCase().contains("T")) {
                tryOutTimes = tryOutTimes.toUpperCase().split("T")[1];
            }
        }
        testModelSubTable.setTryOutTimes(tryOutTimes);
        testModelSubTable.setTryOutDeviceCount(StringUtils.isNumeric(moldCount) ? Integer.valueOf(moldCount) : 0);
        testModelSubTable.setCreateTime(new Date());
        testModelSubTable.setUpdateTime(new Date());
        testModelSubTableService.save(testModelSubTable);

        List<String> imageList = JSONObject.parseArray(imageArray.toJSONString(), String.class);
        //保存图片
        if (Objects.nonNull(imageList) && !imageList.isEmpty()) {
            List<TestImage> testImageList = imageList.stream().map(url -> {
                        TestImage testImage = new TestImage();
                        testImage.setType(2);
                        testImage.setSceneId(noteId);
                        testImage.setUrl(url);
                        return testImage;
                    })
                    .collect(Collectors.toList());
            testImageService.saveBatch(testImageList);
        }
        return JSON.toJSONString(R.success("添加成功"));
    }

    @Override
    @Transactional
    public String deleteNote(JSONObject jsonObject) {
        //jsonobject解析获取试模通知单Id
        String noteId = jsonObject.getString("noteId");
        TestNoticeMainTabale mainTabale = testNoticeMainTabaleService.getById(noteId);
        if (ObjectUtil.isNotNull(mainTabale)) {
            if (StringUtils.isNotBlank(mainTabale.getParentPlanSubId())) {
                testModelSubTableService.removeById(mainTabale.getParentPlanSubId());
            }
            boolean b = testNoticeMainTabaleService.removeById(noteId);
            if (b) {
                return JSON.toJSONString(R.success("删除成功"));
            }
            //删除图片
            testImageMapper.delete(new LambdaUpdateWrapper<TestImage>().eq(TestImage::getType, 2).eq(TestImage::getSceneId, noteId));
        }
        return JSON.toJSONString(R.fail("数据已删除/不存在"));
    }

    @Override
    public R noteDetail(JSONObject jsonObject) {
        //jsonobject解析获取试模通知单Id
        String noteId = jsonObject.getString("noteId");
        NoteDetailVo noteDetailVo = testNoticeMainTabaleMapper.noteDetail(noteId);
        if (ObjectUtil.isNull(noteDetailVo)) {
            return R.success("无数据");
        }
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(noteDetailVo));
        //查询图片信息
        List<TestImage> testImageList = testImageMapper.selectList(new LambdaQueryWrapper<TestImage>()
                .eq(TestImage::getType, 2)
                .eq(TestImage::getSceneId, noteId)
                .orderByAsc(TestImage::getCreateTime));
        if (Objects.nonNull(testImageList) && !testImageList.isEmpty()) {
            object.put("images", testImageList.stream().map(x -> x.getUrl()).collect(Collectors.toList()));
        }else{
            if (StringUtils.isNotEmpty(noteDetailVo.getAnnouncementsImages())) {
                try {
                    object.put("announcementsImages", MyUtils.getStringTransitionList(noteDetailVo.getAnnouncementsImages()));
                    object.put("images", MyUtils.getStringTransitionList(noteDetailVo.getAnnouncementsImages()));
                } catch (Exception e) {
                    object.put("announcementsImages", noteDetailVo.getAnnouncementsImages());
                    object.put("images", noteDetailVo.getAnnouncementsImages());
                }
            }
        }

        if (StringUtils.isNotBlank(noteDetailVo.getTongmanId())) {
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(Integer.valueOf(noteDetailVo.getTongmanId()));
            jsonArray.add(Integer.valueOf(noteDetailVo.getTongmanName()));
            object.put("tongmanName", jsonArray);
        } else {
            object.put("tongmanName", new JSONArray());
        }
        if (StringUtils.isNotBlank(noteDetailVo.getTryOutTimes())) {
            object.put("tryOutTimes", "T" + noteDetailVo.getTryOutTimes());
        }
        if (StringUtils.isBlank(noteDetailVo.getNoteName())) {
            object.put("noteName", noteDetailVo.getMoldName() + "_T" + noteDetailVo.getTryOutTimes() + "_试模通知单");
        }
        object.put("status", NoticeMainEnum.getMessageName(noteDetailVo.getStatus()));
        object.put("tryoutStatus", NoticeMainEnum.getMessage(noteDetailVo.getStatus()));
        TestModelSubTable one = testModelSubTableService
                .lambdaQuery()
                .eq(TestModelSubTable::getPlanId, noteDetailVo.getPlanId())
                .one();
        if (StringUtils.isNotBlank(one.getMoldName())) {
            object.put("moldName", one.getMoldName());
        }
        if (StringUtils.isNotBlank(one.getMoldLook())) {
            object.put("moldLook", one.getMoldLook());
        }
        if (StringUtils.isNotBlank(one.getPourTime())) {
            object.put("pourTime", one.getPourTime());
        }
        if (StringUtils.isNotBlank(one.getTemperatureControlTime())) {
            object.put("temperatureControlTime", one.getTemperatureControlTime());
        }
        if (StringUtils.isNotBlank(one.getNeedHydraumaticStation())) {
            object.put("needHydraumaticStation", one.getNeedHydraumaticStation());
        }
        if (StringUtils.isNotBlank(one.getInjectionMoldingTime())) {
            object.put("injectionMoldingTime", one.getInjectionMoldingTime());
        }
        if (StringUtils.isNotBlank(one.getHasLimitSwitch())) {
            object.put("hasLimitSwitch", one.getHasLimitSwitch());
        }
        if (StringUtils.isNotBlank(one.getHotSpotPairType())) {
            object.put("hotSpotPairType", one.getHotSpotPairType());
        }
        if (StringUtils.isNotBlank(one.getHasTimeRelay())) {
            object.put("hasTimeRelay", one.getHasTimeRelay());
        }
        if (StringUtils.isNotBlank(one.getMaterials())) {
            object.put("materials", one.getMaterials());
        }
        if (StringUtils.isNotBlank(one.getMoldCount())) {
            object.put("moldCount", one.getMoldCount());
        }
        if (StringUtils.isNotBlank(one.getColor())) {
            object.put("color", one.getColor());
        }
        if (StringUtils.isNotBlank(one.getMaterialsCount())) {
            object.put("materialsCount", one.getMaterialsCount());
        }
        if (StringUtils.isNotBlank(one.getSamplePieceWeight())) {
            object.put("samplePieceWeight", one.getSamplePieceWeight());
        }
        if (StringUtils.isNotBlank(one.getMoldWeight())) {
            object.put("moldWeight", one.getMoldWeight());
        }
        return R.data(object);
    }

    @Override
    public synchronized String applyToTryOut(JSONObject jsonObject, SystemHeader header) {
        //jsonobject解析获取试模通知单Id
        String noteId = jsonObject.getString("noteId");

        TestNoticeMainTabale mainTabale = testNoticeMainTabaleService.getById(noteId);
        if (ObjectUtil.isNull(mainTabale)) {
            return JSON.toJSONString(R.fail(400, "试模通知单已删除/不存在"));
        }

        switch (mainTabale.getStatus()) {
            case "1":
            case "2":
            case "3":
            case "4":
                return JSON.toJSONString(R.fail(500, "该通知单开始试模，不可提交"));
            default:
                break;
        }

        TestModelSubTable subTable = testModelSubTableService.getById(mainTabale.getParentPlanSubId());
        if (ObjectUtil.isNull(subTable)) {
            return JSON.toJSONString(R.fail(400, "试模通知单已删除/不存在"));
        }

        TestModelParam modelParam = testModelParamService.getById(subTable.getMoldId());
        if (ObjectUtil.isNull(modelParam)) {
            return JSON.toJSONString(R.fail(400, "该通知单试模模具已删除/不存在"));
        }
        //版本号
        Integer version = mainTabale.getEdition() + 1;
        JSONObject json = new JSONObject();
        try {
            String tn = "";
            if (StringUtils.isNumeric(subTable.getTongmanName())) {
                tn = oaUserService.getOaUserName(subTable.getTongmanName());
            } else {
                tn = subTable.getTongmanName();
            }
            //当前版次的所有信息
            JSONObject edtion = EdtionUtil.createNoticeEdtion(subTable, mainTabale, tn);
            EdtionMessage edtionMessage = new EdtionMessage();
            edtionMessage.setRelId(mainTabale.getId());
            edtionMessage.setType(1);
            edtionMessage.setEdtion(version);
            edtionMessage.setRemark(edtion.toJSONString());
            edtionMessageService.remove(new LambdaQueryWrapper<EdtionMessage>()
                    .eq(EdtionMessage::getRelId, mainTabale.getId())
                    .eq(EdtionMessage::getEdtion, version)
            );
            edtionMessageService.save(edtionMessage);
            if (mainTabale.getEdition() != 0) {
                EdtionMessage message = edtionMessageService
                        .lambdaQuery()
                        .eq(EdtionMessage::getRelId, mainTabale.getId())
                        .eq(EdtionMessage::getEdtion, mainTabale.getEdition())
                        .one();
                if (ObjectUtil.isNotEmpty(message)) {
                    List<Edtion> edtions = verityEdtion(message.getRemark(), mainTabale, subTable, version, header.getId());
                    if (CollectionUtil.isNotEmpty(edtions)) {
                        edtionService.saveBatch(edtions);
                    }
                }
            }

            mainTabale.setStatus(NoticeMainEnum.WAIT_APPROVAL.getCode());
            testNoticeMainTabaleService.lambdaUpdate()
                    .set(TestNoticeMainTabale::getStatus, NoticeMainEnum.WAIT_APPROVAL.getCode())
                    .set(TestNoticeMainTabale::getUpdateTime, new Date())
                    .eq(TestNoticeMainTabale::getId, mainTabale.getId())
                    .update();
            //提交数据的封装
            TestModeApi modeApi = new TestModeApi();
            modeApi.setMouldNumber(MyUtils.getString(modelParam.getMoldId()));
            modeApi.setMouldName(MyUtils.getString(modelParam.getMoldName()));
            modeApi.setMouldAppearance(MyUtils.getString(modelParam.getMoldLook()));
            modeApi.setNumberOfTemperatureControlLocations(MyUtils.getString(modelParam.getTemperatureControlTime()));
            modeApi.setNumberOfGates(MyUtils.getInteger(MyUtils.getString(modelParam.getPourTime())));
            modeApi.setThermocoupleModel(MyUtils.getString(modelParam.getHotSpotPairType()));
            modeApi.setRequireHydraulicWorkstation(MyUtils.getBoolean(modelParam.getNeedHydraumaticStation()));
            modeApi.setNumberOfSequentialGates(MyUtils.getInteger(modelParam.getInjectionMoldingTime()));
            modeApi.setHasLimitSwitch(MyUtils.getBoolean(modelParam.getHasLimitSwitch()));
            modeApi.setHasTimeRelay(MyUtils.getBoolean(modelParam.getHasTimeRelay()));
            TryOutDeviceMod deviceMod = tryOutDeviceModMapper.selectOne(new LambdaUpdateWrapper<TryOutDeviceMod>()
                    .eq(TryOutDeviceMod::getDeviceId, mainTabale.getTryOutDeviceId())
                    .last("LIMIT 1")
            );
            if (ObjectUtil.isNotEmpty(deviceMod)) {
                modeApi.setMachine(MyUtils.getString(deviceMod.getDeviceName()));
            } else {
                modeApi.setMachine("");
            }
            modeApi.setProductMaterial(MyUtils.getString(modelParam.getMaterials()));
            modeApi.setSampleQuantity(subTable.getTryOutDeviceCount());
            modeApi.setProductColor(MyUtils.getString(modelParam.getColor()));
            modeApi.setMaterialConsumption(MyUtils.getString(modelParam.getMaterialWeight()));
            modeApi.setSampleWeight(MyUtils.getString(modelParam.getSamplePieceWeight()));
            modeApi.setTestingStage("T" + subTable.getTryOutTimes());
            modeApi.setPlanTime(MyUtils.dataToIso8601(subTable.getPlannedTime()));
            modeApi.setMouldWeight(MyUtils.getString(modelParam.getMoldWeight()));
            if (StringUtils.isNumeric(subTable.getTongmanName())) {
                OaUserParam oaUserParam = oaUserService.getById(subTable.getTongmanName());
                modeApi.setFitterLeader(oaUserParam.getName());
            } else {
                modeApi.setFitterLeader(subTable.getTongmanName());
            }
            DictTable dictTable = dictTableMapper.selectOne(new LambdaUpdateWrapper<DictTable>()
                    .eq(DictTable::getTypeName, "试模目的")
                    .eq(DictTable::getDicId, mainTabale.getTargetId())
                    .last("LIMIT 1")
            );
            if (ObjectUtil.isNotNull(dictTable)) {
                modeApi.setPurpose(MyUtils.getString(dictTable.getDicName()));
                modeApi.setPurposeAbbreviation(MyUtils.getString(dictTable.getDicName()));
            } else {
                modeApi.setPurpose("");
                modeApi.setPurposeAbbreviation("");
            }
            dictTable = dictTableMapper.selectOne(new LambdaUpdateWrapper<DictTable>()
                    .eq(DictTable::getTypeName, "试模原因")
                    .eq(DictTable::getDicId, mainTabale.getTryOutReasonId())
                    .last("LIMIT 1")
            );
            if (ObjectUtil.isNotNull(dictTable)) {
                modeApi.setReason(MyUtils.getString(dictTable.getDicName()));
            } else {
                modeApi.setReason("");
            }
            modeApi.setConsiderationsAndRequirements(MyUtils.getString(mainTabale.getAnnouncements()));

            UserApi userApi = userApiMapper.selectById(header.getId());
            OaUserParam byId = oaUserService.getById(userApi.getId());
            //申请人OA用户名
            json.put("senderLoginName", byId.getOaUsername());
            //模具编号
            json.put("mouldNumber", MyUtils.getStringToStr(modeApi.getMouldNumber()));
            //模具名称
            json.put("mouldName", MyUtils.getStringToStr(modeApi.getMouldName()));
            //模具外形
            json.put("mouldAppearance", MyUtils.getStringToStr(modeApi.getMouldAppearance()));
            //几点温控（注明接口）
            json.put("numberOfTemperatureControlLocations", MyUtils.getStringToStr(modeApi.getNumberOfTemperatureControlLocations()));
            //几点浇筑
            json.put("numberOfGates", modeApi.getNumberOfGates());
            //热电偶型号
            json.put("thermocoupleModel", MyUtils.getStringToStr(modeApi.getThermocoupleModel()));
            //是否需要液压工作站
            json.put("requireHydraulicWorkstation", modeApi.getRequireHydraulicWorkstation());
            //几点顺序注塑
            json.put("numberOfSequentialGates", modeApi.getNumberOfSequentialGates());
            //是否有限位开关
            json.put("hasLimitSwitch", modeApi.getHasLimitSwitch());
            //是否有时间继电器
            json.put("hasTimeRelay", modeApi.getHasTimeRelay());
            //试模设备
            json.put("machine", MyUtils.getStringToStr(modeApi.getMachine()));
            //制件材料
            json.put("productMaterial", MyUtils.getStringToStr(modeApi.getProductMaterial()));
            //试模件数
            json.put("sampleQuantity", modeApi.getSampleQuantity());
            //制件颜色
            json.put("productColor", MyUtils.getStringToStr(modeApi.getProductColor()));
            //材料用量
            json.put("materialConsumption", MyUtils.getStringToStr(modeApi.getMaterialConsumption()));
            //样件重量
            json.put("sampleWeight", MyUtils.getStringToStr(modeApi.getSampleWeight()));
            //试模次数
            json.put("testingStage", MyUtils.getStringToStr(modeApi.getTestingStage()));
            //日期格式: ISO 8601
            json.put("planTime", MyUtils.getStringToStr(modeApi.getPlanTime()));
            //模具重量
            json.put("mouldWeight", MyUtils.getStringToStr(modeApi.getMouldWeight()));
            //钳工组长
            json.put("fitterLeader", MyUtils.getStringToStr(modeApi.getFitterLeader()));
            //试模目的
            json.put("purpose", MyUtils.getStringToStr(modeApi.getPurpose()));
            //试模目的缩写
            json.put("purposeAbbreviation", MyUtils.getStringToStr(modeApi.getPurposeAbbreviation()));
            //试模原因
            json.put("reason", MyUtils.getStringToStr(modeApi.getReason()));
            //注意事项及特殊要求
            json.put("considerationsAndRequirements", MyUtils.getStringToStr(modeApi.getConsiderationsAndRequirements()));
            System.out.println(json.toJSONString());
        } catch (Exception e) {
            UpdateWrapper<TestNoticeMainTabale> wrapper = new UpdateWrapper<>();
            wrapper.lambda()
                    .set(TestNoticeMainTabale::getStatus, NoticeMainEnum.APPROVAL_ERROR.getCode())
                    .set(TestNoticeMainTabale::getUpdateTime, new Date())
                    .eq(TestNoticeMainTabale::getId, mainTabale.getId());
            testNoticeMainTabaleMapper.update(null, wrapper);
            return JSON.toJSONString(R.fail(400, "审批推送异常：" + e.getMessage()));
        }
        try {
            HttpResponse login = ApiUtilServiceImpl.login(keyWord.getApi());
            if (login.getStatus() != org.springframework.http.HttpStatus.OK.value()) {
                log.info("通知单提交,与OA服务系统连接异常:" + login.body());
                return JSON.toJSONString(R.fail("与OA服务系统连接异常，请稍后重试"));
            }
            log.info("通知单提交：" + json.toJSONString());
            HttpResponse httpResponse = ApiUtilServiceImpl.doPost(keyWord.getApi() + ApiUtilServiceImpl.CREATE_NOTICE, json, login.body());
            log.info("通知单状态：" + httpResponse.getStatus() + ",内容：" + httpResponse.body());
            if (httpResponse.getStatus() == HttpStatus.HTTP_OK) {
                UpdateWrapper<TestNoticeMainTabale> wrapper = new UpdateWrapper<>();
                wrapper.lambda().set(TestNoticeMainTabale::getStatus, NoticeMainEnum.APPROVAL_DOING.getCode())
                        .set(TestNoticeMainTabale::getApprovalNumber, httpResponse.body())
                        .set(TestNoticeMainTabale::getEdition, version)
                        .set(TestNoticeMainTabale::getUpdateTime, new Date())
                        .eq(TestNoticeMainTabale::getId, mainTabale.getId());
                testNoticeMainTabaleMapper.update(null, wrapper);
                return JSON.toJSONString(R.success("提交成功"));
            } else {
                UpdateWrapper<TestNoticeMainTabale> wrapper = new UpdateWrapper<>();
                wrapper.lambda()
                        .set(TestNoticeMainTabale::getStatus, NoticeMainEnum.APPROVAL_ERROR.getCode())
                        .set(TestNoticeMainTabale::getUpdateTime, new Date())
                        .eq(TestNoticeMainTabale::getId, mainTabale.getId());
                testNoticeMainTabaleMapper.update(null, wrapper);
                return JSON.toJSONString(R.fail(httpResponse.getStatus(), httpResponse.body()));
            }
        } catch (Exception e) {
            UpdateWrapper<TestNoticeMainTabale> wrapper = new UpdateWrapper<>();
            wrapper.lambda()
                    .set(TestNoticeMainTabale::getStatus, NoticeMainEnum.APPROVAL_ERROR.getCode())
                    .set(TestNoticeMainTabale::getUpdateTime, new Date())
                    .eq(TestNoticeMainTabale::getId, mainTabale.getId());
            testNoticeMainTabaleMapper.update(null, wrapper);
            return JSON.toJSONString(R.fail(400, "审批推送异常：" + e.getMessage()));
        }
    }

    private List<Edtion> verityEdtion(String message, TestNoticeMainTabale mainTabale, TestModelSubTable subTable, int version, String userId) {
        JSONObject json = JSONObject.parseObject(message);
        List<Edtion> list = new ArrayList<>();
        if (StringUtils.isNotBlank(mainTabale.getVersionId()) && !json.getString("版本").equals(mainTabale.getVersionId())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("versionId");
            edtion.setKeyValue("版本");
            DictTable dictTable = dictTableMapper.selectById(json.getString("版本"));
            if (ObjectUtil.isEmpty(dictTable)) {
                edtion.setBaseValue("");
            } else {
                edtion.setBaseValue(dictTable.getDicName());
            }
            DictTable dictTable1 = dictTableMapper.selectById(mainTabale.getVersionId());
            if (ObjectUtil.isEmpty(dictTable1)) {
                edtion.setUpdateValue("");
            } else {
                edtion.setUpdateValue(dictTable1.getDicName());
            }
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(mainTabale.getNoteName()) && !json.getString("通知单名称").equals(mainTabale.getNoteName())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("note_name");
            edtion.setKeyValue("通知单名称");
            edtion.setBaseValue(json.getString("通知单名称"));
            edtion.setUpdateValue(mainTabale.getNoteName());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(mainTabale.getSerialNumber()) && !json.getString("编号").equals(mainTabale.getSerialNumber())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("serial_number");
            edtion.setKeyValue("编号");
            edtion.setBaseValue(json.getString("编号"));
            edtion.setUpdateValue(mainTabale.getSerialNumber());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(mainTabale.getTargetId()) && !json.getString("试模目的").equals(mainTabale.getTargetId())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("targetId");
            edtion.setKeyValue("试模目的");
            DictTable dictTable = dictTableMapper.selectById(json.getString("试模目的"));
            if (ObjectUtil.isEmpty(dictTable)) {
                edtion.setBaseValue("");
            } else {
                edtion.setBaseValue(dictTable.getDicName());
            }
            DictTable dictTable1 = dictTableMapper.selectById(mainTabale.getTargetId());
            if (ObjectUtil.isEmpty(dictTable1)) {
                edtion.setUpdateValue("");
            } else {
                edtion.setUpdateValue(dictTable1.getDicName());
            }
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(mainTabale.getTryOutReasonId()) && !json.getString("试模原因").equals(mainTabale.getTryOutReasonId())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("tryOutReasonId");
            edtion.setKeyValue("试模原因");
            DictTable dictTable = dictTableMapper.selectById(json.getString("试模原因"));
            if (ObjectUtil.isEmpty(dictTable)) {
                edtion.setBaseValue("");
            } else {
                edtion.setBaseValue(dictTable.getDicName());
            }
            DictTable dictTable1 = dictTableMapper.selectById(mainTabale.getTryOutReasonId());
            if (ObjectUtil.isEmpty(dictTable1)) {
                edtion.setUpdateValue("");
            } else {
                edtion.setUpdateValue(dictTable1.getDicName());
            }
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(mainTabale.getAnnouncements()) && !json.getString("试模注意事项").equals(mainTabale.getAnnouncements())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("announcements");
            edtion.setKeyValue("试模注意事项");
            edtion.setBaseValue(json.getString("试模注意事项"));
            edtion.setUpdateValue(mainTabale.getAnnouncements());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(mainTabale.getSubTarget()) && !json.getString("副试模目的").equals(mainTabale.getSubTarget())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("subTarget");
            edtion.setKeyValue("副试模目的");
            edtion.setBaseValue(json.getString("副试模目的"));
            edtion.setUpdateValue(mainTabale.getSubTarget());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(mainTabale.getTryOutDeviceId()) && !json.getString("试模设备").equals(mainTabale.getTryOutDeviceId())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("tryOutDeviceId");
            edtion.setKeyValue("试模设备");
            TryOutDeviceMod deviceMod = tryOutDeviceModMapper.selectById(json.getString("试模设备"));
            if (ObjectUtil.isEmpty(deviceMod)) {
                edtion.setBaseValue("");
            } else {
                edtion.setBaseValue(deviceMod.getDeviceName());
            }
            TryOutDeviceMod deviceMod1 = tryOutDeviceModMapper.selectById(mainTabale.getTryOutDeviceId());
            if (ObjectUtil.isEmpty(deviceMod1)) {
                edtion.setUpdateValue("");
            } else {
                edtion.setUpdateValue(deviceMod1.getDeviceName());
            }
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getMoldId()) && !json.getString("模具编号").equals(subTable.getMoldId())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("moldId");
            edtion.setKeyValue("模具编号");
            edtion.setBaseValue(json.getString("模具编号"));
            edtion.setUpdateValue(subTable.getMoldId());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getMoldName()) && !json.getString("模具名称").equals(subTable.getMoldName())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("moldName");
            edtion.setKeyValue("模具名称");
            edtion.setBaseValue(json.getString("模具名称"));
            edtion.setUpdateValue(subTable.getMoldName());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getMoldLook()) && !json.getString("模具外形").equals(subTable.getMoldLook())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("moldLook");
            edtion.setKeyValue("模具外形");
            edtion.setBaseValue(json.getString("模具外形"));
            edtion.setUpdateValue(subTable.getMoldLook());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getPourTime()) && !json.getString("几点浇注").equals(subTable.getPourTime())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("pourTime");
            edtion.setKeyValue("几点浇注");
            edtion.setBaseValue(json.getString("几点浇注"));
            edtion.setUpdateValue(subTable.getPourTime());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getTemperatureControlTime()) && !json.getString("几点温控（注明接口）").equals(subTable.getTemperatureControlTime())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("temperatureControlTime");
            edtion.setKeyValue("几点温控（注明接口）");
            edtion.setBaseValue(json.getString("几点温控（注明接口）"));
            edtion.setUpdateValue(subTable.getTemperatureControlTime());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getNeedHydraumaticStation()) && !json.getString("是否需要液压工作站").equals(subTable.getNeedHydraumaticStation())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("needHydraumaticStation");
            edtion.setKeyValue("是否需要液压工作站");
            edtion.setBaseValue(json.getString("是否需要液压工作站"));
            edtion.setUpdateValue(subTable.getNeedHydraumaticStation());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getInjectionMoldingTime()) && !json.getString("几点顺序注塑").equals(subTable.getInjectionMoldingTime())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("injectionMoldingTime");
            edtion.setKeyValue("几点顺序注塑");
            edtion.setBaseValue(json.getString("几点顺序注塑"));
            edtion.setUpdateValue(subTable.getInjectionMoldingTime());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getHasLimitSwitch()) && !json.getString("是否有限位开关").equals(subTable.getHasLimitSwitch())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("hasLimitSwitch");
            edtion.setKeyValue("是否有限位开关");
            edtion.setBaseValue(json.getString("是否有限位开关"));
            edtion.setUpdateValue(subTable.getHasLimitSwitch());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getHotSpotPairType()) && !json.getString("热点偶型号").equals(subTable.getHotSpotPairType())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("hotSpotPairType");
            edtion.setKeyValue("热点偶型号");
            edtion.setBaseValue(json.getString("热点偶型号"));
            edtion.setUpdateValue(subTable.getHotSpotPairType());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getHasTimeRelay()) && !json.getString("是否有时间继电器").equals(subTable.getHasTimeRelay())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("hasTimeRelay");
            edtion.setKeyValue("是否有时间继电器");
            edtion.setBaseValue(json.getString("是否有时间继电器"));
            edtion.setUpdateValue(subTable.getHasTimeRelay());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getMaterials()) && !json.getString("制件材料").equals(subTable.getMaterials())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("materials");
            edtion.setKeyValue("制件材料");
            edtion.setBaseValue(json.getString("制件材料"));
            edtion.setUpdateValue(subTable.getMaterials());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getMoldCount()) && !json.getString("试模件数").equals(subTable.getMoldCount())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("moldCount");
            edtion.setKeyValue("试模件数");
            edtion.setBaseValue(json.getString("试模件数"));
            edtion.setUpdateValue(subTable.getMoldCount());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getColor()) && !json.getString("制件颜色").equals(subTable.getColor())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("color");
            edtion.setKeyValue("制件颜色");
            edtion.setBaseValue(json.getString("制件颜色"));
            edtion.setUpdateValue(subTable.getColor());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getMaterialsCount()) && !json.getString("材料重量").equals(subTable.getMaterialsCount())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("materialsCount");
            edtion.setKeyValue("材料重量");
            edtion.setBaseValue(json.getString("材料重量"));
            edtion.setUpdateValue(subTable.getMaterialsCount());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getSamplePieceWeight()) && !json.getString("样件重量").equals(subTable.getSamplePieceWeight())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("samplePieceWeight");
            edtion.setKeyValue("样件重量");
            edtion.setBaseValue(json.getString("样件重量"));
            edtion.setUpdateValue(subTable.getSamplePieceWeight());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getMoldWeight()) && !json.getString("模具重量").equals(subTable.getMoldWeight())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("moldWeight");
            edtion.setKeyValue("模具重量");
            edtion.setBaseValue(json.getString("模具重量"));
            edtion.setUpdateValue(subTable.getMoldWeight());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getTongmanName()) && !json.getString("钳工组长").equals(subTable.getTongmanName())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("tongmanName");
            edtion.setKeyValue("钳工组长");
            edtion.setBaseValue(oaUserService.getOaUserName(json.getString("钳工组长")));
            edtion.setUpdateValue(oaUserService.getOaUserName(subTable.getTongmanName()));
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (StringUtils.isNotBlank(subTable.getTryOutTimes()) && !json.getString("试模次数").equals("T" + subTable.getTryOutTimes())) {
            Edtion edtion = new Edtion();
            edtion.setRed_id(mainTabale.getId());
            edtion.setEdition("V" + version);
            edtion.setKeyName("tryOutTimes");
            edtion.setKeyValue("试模次数");
            edtion.setBaseValue(json.getString("试模次数"));
            edtion.setUpdateValue("T" + subTable.getTryOutTimes());
            edtion.setPerson(userId);
            list.add(edtion);
        }
        if (ObjectUtil.isNotEmpty(subTable.getPlannedTime())) {
            if (!json.getString("计划试模时间").equals(DateUtil.format(subTable.getPlannedTime(), "yyyy-MM-dd"))) {
                Edtion edtion = new Edtion();
                edtion.setRed_id(mainTabale.getId());
                edtion.setEdition("V" + version);
                edtion.setKeyName("plannedTime");
                edtion.setKeyValue("计划试模时间");
                edtion.setBaseValue(json.getString("计划试模时间"));
                edtion.setUpdateValue(DateUtil.format(subTable.getPlannedTime(), "yyyy-MM-dd"));
                edtion.setPerson(userId);
                list.add(edtion);
            }
        }
        if (CollectionUtil.isNotEmpty(list)) {
            return list;
        }
        return null;
    }

}
